﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Declaration of CountryCodes static class
	/// </summary>
	public static class CountryCodes
	{

		/////////////////////////////////////////////////////////////////
		// Static Attributes

		// Only want to create this list once, and it's not going to change
		private static Dictionary<string,string> m_dictionary;
		public static Dictionary<string, string> Dictionary
		{
			get {
#if DEBUG
				Trace.Assert(m_dictionary != null);
				Trace.Assert(m_dictionary.Count > 0);

#endif // #if DEBUG
				return m_dictionary; 
			}
		}

		/////////////////////////////////////////////////////////////
		// Static Operations

		public static void LoadUp(DebtChasedEntities ent)
		{
#if DEBUG
			Trace.Assert(m_dictionary == null);
#endif // #if DEBUG

			// Load the countries into a dictionary
			m_dictionary = new Dictionary<string, string>();
			//foreach (Country c in ent.Countries)
				m_dictionary.Add("England", "UK");

		}

		public static string CodeToName(string strCode)
		{

			// shortcuts
			if ((strCode == null) || (strCode.Length != 2))
				return null;

			// Declare return variable
			string strFtmp = null;
			if(m_dictionary.ContainsKey(strCode))			
				strFtmp = m_dictionary[strCode];

			// Return variable
			return strFtmp;

		}

		public static string NameToCode(string strName, string strDefaultCountryCode)
		{

			// shortcuts
			if (string.IsNullOrEmpty(strName))
				return strDefaultCountryCode;

			// Declare return variable
			string strFtmp = null;

			// Chars that name may be split by
			char[] cSplits = new char[3] { ',', '/', '-' };

			// Find the key
			strFtmp = (from p in m_dictionary where (string.Compare(p.Value,strName,true)==0) select p.Key).FirstOrDefault();
			if (string.IsNullOrEmpty(strFtmp))
			{

				// Check special names
				if (strName.Length == 2)
				{

					// This might be a valid code
					if (m_dictionary.ContainsKey(strName))
						strFtmp = strName;

				}
				else if (strName.IndexOfAny(cSplits) != -1)
				{

					// Break string by commas and slashes
					string[] strList = strName.Split(cSplits);
					foreach (string str1 in strList)
					{
						strFtmp = NameToCode(str1.Trim(), null);
						if (strFtmp != null)
							break;
					}

				}

				// If code not found...
				if (strFtmp == null)
				{
					if (string.Compare(strName, "USA", true) == 0)
						strFtmp = "US";
					else if (string.Compare(strName, "Russia", true) == 0)
						strFtmp = NameToCode("Russian Federation", null);
					else if (strName.StartsWith("Bosnia", StringComparison.CurrentCultureIgnoreCase))
						strFtmp = NameToCode("Trinidad", null);
					else if (strName.StartsWith("Trinidad", StringComparison.CurrentCultureIgnoreCase))
						strFtmp = NameToCode("Trinidad and Tobago", null);
					else if (strName.IndexOf("Ireland", StringComparison.CurrentCultureIgnoreCase) != -1)
						strFtmp = NameToCode("Ireland", null);
				}

				// If not found...
				if (string.IsNullOrEmpty(strFtmp))
					strFtmp = strDefaultCountryCode;

			}

			// Return variable
			return strFtmp;

		}

	}

}